package com.ezhanpple.colorpicker

/**
 * Created by lucas
 * Date: 2020/5/7 20:13
 * 与Ebo相关的工具类
 */
object EboUtils {
    private const val TAG = "EboUtils"

    fun isNewVersion(deviceVersion: String, targetVersion: String): Boolean {
        val compareEboFirmwareVersion = compareEboFirmwareVersion(deviceVersion, targetVersion)
        println("compareEboFirmwareVersion--  $compareEboFirmwareVersion")
        return compareEboFirmwareVersion >= 0
    }

    fun compareEboFirmwareVersion(deviceVersion: String, targetVersion: String): Int {
        var currentVersion = deviceVersion
        var compareVersion = targetVersion
        if (!isEmpty(currentVersion)) {
            val splitOld = currentVersion.split("-").toTypedArray()
            if (splitOld.isNotEmpty()) {
                currentVersion =
                    if (splitOld[0].startsWith("V") || splitOld[0].startsWith("v")) {
                        splitOld[0].substring(1)
                    } else {
                        splitOld[0]
                    }
            }
        }
        if (!isEmpty(compareVersion)) {
            val splitNew = compareVersion.split("-").toTypedArray()
            if (splitNew.isNotEmpty()) {
                compareVersion =
                    if (splitNew[0].startsWith("V") || splitNew[0].startsWith("v")) {
                        splitNew[0].substring(1)
                    } else {
                        splitNew[0]
                    }
            }
        }
        println("$currentVersion --- $compareVersion")
        val version1Array = currentVersion.split("\\.".toRegex()).toTypedArray()
        val version2Array = compareVersion.split("\\.".toRegex()).toTypedArray()
        val minLen = version1Array.size.coerceAtMost(version2Array.size)
        for (index in 0 until minLen) {
            val trimOld = version1Array[index].trim()
            val trimNew = version2Array[index].trim()
            println("$trimOld --- $trimNew")
            val integerNew = trimOld.toVersionInt()
            val integerOld = trimNew.toVersionInt()
            val compareTo: Int = integerNew - integerOld
            if (compareTo != 0) {
                return compareTo
            }
        }
        when {
            version1Array.size > version2Array.size -> {
                for (index in minLen until version1Array.size) {
                    val s = version1Array[index].toVersionInt()
                    if (s != 0) {
                        return 1
                    }
                }
                return 0
            }
            version1Array.size < version2Array.size -> {
                for (index in minLen until version2Array.size) {
                    val s = version2Array[index].toVersionInt()
                    if (s != 0) {
                        return -s
                    }
                }
                return 0
            }
            else -> {
                return 0
            }
        }
    }

    private fun isEmpty(str: CharSequence?): Boolean {
        return str == null || str.isEmpty()
    }


    private fun String.toVersionInt(): Int {
        return if (isBlank()) {
            0
        } else {
            try {
                toInt()
            } catch (e: Exception) {
                compareTo("0")
            }
        }
    }
}